package com.itheima.core.web.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.common.utils.Page;
import com.itheima.core.po.BaseDict;
import com.itheima.core.po.Customer;
import com.itheima.core.po.User;
import com.itheima.core.service.BaseDictService;
import com.itheima.core.service.CustomerService;

/**
 *	@author zangye03
 *	date Jul 17, 20215:02:54 PM
 *	Description	 
 *	客户管理控制类
 */

@Controller
public class CustomerController {
	@Autowired
	private CustomerService customerService;
	@Autowired
	private BaseDictService baseDictService;
	// 客户来源
	@Value("${customer.from.type}")
	private String FROM_TYPE;
	// 客户所属行业
	@Value("${customer.industry.type}")
	private String INDUSTRY_TYPE;
	// 客户级别
	@Value("${customer.level.type}")
	private String LEVEL_TYPE;
	
	/**
	 * 客户列表
	 * @param page
	 * @param rows
	 * @param custName
	 * @param custSource
	 * @param custIndustry
	 * @param custLevel
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/customer/list.action")
	public String list(@RequestParam(defaultValue="1") Integer page,
			@RequestParam(defaultValue="10") Integer rows,
			String custName, String custSource, String custIndustry,
			String custLevel, Model model) {
		// 条件查询所有客户
		Page<Customer> customers = customerService
				.findCustomerList(page, rows, custName, custSource, custIndustry, custLevel);
		model.addAttribute("page",customers);
		
		System.out.println(customers.toString());
		
		
		// 客户来源
		List<BaseDict> fromType = baseDictService
				.findBaseByTypeCode(FROM_TYPE);
		System.out.println(fromType.toString());
		
		// 客户所属行业
		List<BaseDict> industryType = baseDictService
				.findBaseByTypeCode(INDUSTRY_TYPE);
		System.out.println(industryType.toString());
		
		// 客户级别
		List<BaseDict> levelType = baseDictService
				.findBaseByTypeCode(LEVEL_TYPE);
		System.out.println(levelType.toString());
		
		// 添加参数
		model.addAttribute("fromType",fromType);
		model.addAttribute("industryType",industryType);
		model.addAttribute("levelType",levelType);
		model.addAttribute("custName",custName);
		model.addAttribute("custSource",custSource);
		model.addAttribute("custIndustry",custIndustry);
		model.addAttribute("custLevel",custLevel);
		return "customer";
	}
	
	/**
	 * 创建客户
	 * @param customer
	 * @param session
	 * @return
	 */
	@RequestMapping("/customer/create.action")
	@ResponseBody
	public String customerCreate(Customer customer,HttpSession session) {
		// 获取Session中的当前用户信息
		User user = (User) session.getAttribute("USER_SESSION");
		// 将当前用户id存储在客户对象中
		customer.setCustUserId(user.getUserId());
		// 创建Date对象
		Date date = new Date();
		// 得到一个Timestamp格式的时间,存入mysql中的时间格式"yyyy:MM/dd HH:mm:ss"
		Timestamp timeStamp = new Timestamp(date.getTime());
		customer.setCustCreatetime(timeStamp);
		// 执行Service层中的创建方法,返回的是受影响的行数
		int rows = customerService.createCustomer(customer);
		if(rows > 0) {
			return "OK";
		}else {
			return "FALL";
		}
	}
	
	/**
	 * 通过id获取客户信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/customer/getCustomerById.action")
	@ResponseBody
	public Customer getCustomerById(Integer id) {
		Customer customer = customerService.getCustomerById(id);
		return customer;
	}
	
	/**
	 * 更新客户
	 * @param customer
	 * @return
	 */
	@RequestMapping("/customer/update.action")
	@ResponseBody
	public String customerUpdate(Customer customer) {
		int rows = customerService.updateCustomer(customer);
		if(rows > 0) {
			return "OK";
		}else {
			return "FALL";
		}
	}
	
	/**
	 * 删除客户 
	 * @param id
	 * @return
	 */
	@RequestMapping("/customer/delete.action")
	@ResponseBody
	public String customerDelete(Integer id) {
		int rows = customerService.deleteCustomer(id);
		if(rows > 0) {
			return "OK";
		}else {
			return "FALL";
		}
	}
}
